package com.itic.web.question.studyrecord.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.itic.appbase.applications.sys.common.service.OutCommonService;
import com.itic.appbase.framework.common.constants.DBConst;
import com.itic.appbase.framework.plugins.mybatis.Page;
import com.itic.appbase.framework.utils.StringHelper;
import com.itic.demo.modelfiles.service.ModelFilesService;
import com.itic.ht.cms.question.persistence.model.QuestionModel;
import com.itic.ht.cms.question.persistence.model.QuestionModelExample;
import com.itic.ht.cms.question.service.QuestionModelService;
import com.itic.ht.cms.questionselection.persistence.model.QuestionSelection;
import com.itic.ht.cms.questionselection.persistence.model.QuestionSelectionExample;
import com.itic.ht.cms.questionselection.service.QuestionSelectionService;
import com.itic.ht.cms.questype.persistence.model.QuestionType;
import com.itic.ht.cms.questype.persistence.model.QuestionTypeExample;
import com.itic.ht.cms.questype.service.QuestionTypeService;
import com.itic.ht.mem.member.persistence.model.WebMember;
import com.itic.ht.mem.member.service.WebMemberService;
import com.itic.web.qo.TokenData;
import com.itic.web.ro.PageInfo;
import com.itic.web.ro.ResResult;
import com.itic.web.util.TokenUtil;

@Service
@Transactional
public class AppRecordService {
	

	@Autowired
	private WebMemberService webMemberService;
    @Autowired
    private ModelFilesService modelFilesService;
    @Autowired
    protected QuestionTypeService questionTypeService;
	@Autowired
	protected QuestionModelService questionModelService;
	@Autowired
	protected QuestionSelectionService questionSelectionService;
    @Autowired
   	protected OutCommonService outCommonService;

	
	public ResResult getTypeList(TokenData data){
		
		WebMember member = null;
		try {
			//判断token是不是存在
			if(StringHelper.isEmpty(data.getToken())){
				return ResResult.error(1, "token不能为空");
			}
			//先判断token正确性
			boolean flag = TokenUtil.assertUserToken(data);
			if(!flag){
				return ResResult.error(2, "token失效");
			}
			
			//初始化返回值
			Map<String, Object> datas = new HashMap<String, Object>();
			
			//从token中取account信息   
			data.setAccount(TokenUtil.getUserList(data.getToken()).getAccount().trim());
			//取得用户信息
			member = webMemberService.getMemberByAccount(data.getAccount());
			System.out.println("查询到的用户帐号是"+member.getAccount());
			
			// 获取数据
			QuestionTypeExample typeExample = new QuestionTypeExample();
			typeExample.createCriteria().andIsEffectiveEqualTo(DBConst.TRUE).andParentIdEqualTo(DBConst.FALSE);
	        
			List<QuestionType> typelist = questionTypeService.list(typeExample);
            for (QuestionType webVedio : typelist) {
            	String fileId = webVedio.getFileId();
            	if(fileId != null && !"".equals(fileId)){
            		webVedio.setFileId(modelFilesService.getFile(fileId).getFilePath());
            	}
			}
            
            member.setPassword(null);
            member.setExpireTime(null);
            member.setVerificationCode(null);
            member.setVerificationExpireTime(null);
            
            datas.put("member", member);
            datas.put("QuestionTypes", typelist);
            
			return ResResult.build(200, "会员卡列表查询成功",datas);
		} catch (Exception e) {
			//返回错误代码
			e.printStackTrace();
			return ResResult.error(4, "会员卡列表查询失败");
		}

	}
	
	public ResResult getQuestionInfo(TokenData data,String CategoryId,Page page){
		
		WebMember member = null;
		try {
			//判断token是不是存在
			if(StringHelper.isEmpty(data.getToken())){
				return ResResult.error(1, "token不能为空");
			}
			//先判断token正确性
			boolean flag = TokenUtil.assertUserToken(data);
			if(!flag){
				return ResResult.error(2, "token失效");
			}
			//page对象不能为空
	        if (page.getPageNow()>0 && page.getPageSize()>0) {
	        }else{
	        	return ResResult.error(3, "page数据不能为空");
	        }
			page.setRecordNow((page.getPageNow()-1)*page.getPageSize());
			page.setPageSize(1);
			
			//初始化返回值
			Map<String, Object> datas = new HashMap<String, Object>();
			
			//从token中取account信息   
			data.setAccount(TokenUtil.getUserList(data.getToken()).getAccount().trim());
			//取得用户信息
			member = webMemberService.getMemberByAccount(data.getAccount());
			System.out.println("查询到的用户帐号是"+member.getAccount());
			
			
			//通过类型识别码 ->查到类
			QuestionType modelByType = questionTypeService.getModelByTypeCode(CategoryId);
			
			//先查问题1
			QuestionModel questionModel = null;
			List<QuestionSelection> QuestionSelectionList = new ArrayList<QuestionSelection>();
			QuestionSelection selectResult = null;
			
			QuestionModelExample questionExample = new QuestionModelExample();
			questionExample.createCriteria().andIsEffectiveEqualTo(DBConst.TRUE).andCategoryIdEqualTo(modelByType.getId());
			
			questionExample.setPage(page);
			int total = questionModelService.count(questionExample);
			List<QuestionModel> questionList = questionModelService.list(questionExample);
			if(questionList!=null && questionList.size()>0){
				  questionModel = questionList.get(0);
				  String fileId = questionModel.getFileId();
				  if(fileId != null && !"".equals(fileId)){
						questionModel.setFileId(modelFilesService.getFile(fileId).getFilePath());
				  }
			}
			//根据问题查询选项 查询提示
			if(questionModel != null){
				QuestionSelectionExample questionSelectionExample = new QuestionSelectionExample();
				questionSelectionExample.createCriteria().andIsEffectiveEqualTo(DBConst.TRUE).andQuestionIdEqualTo(questionModel.getId());
				List<QuestionSelection> selectlist = questionSelectionService.list(questionSelectionExample);
                for (QuestionSelection questionSelection : selectlist) {
					if(questionSelection.getSelectType().equals("XX")){
						QuestionSelectionList.add(questionSelection);
					}
					if(questionSelection.getSelectType().equals("TS")){
						selectResult = questionSelection;
					}
				}
				
			}
			
			//最后查询分页信息
			PageInfo instance = PageInfo.getInstance(page.getPageNow(), page.getPageSize(), total);
			
            datas.put("member", member);
            datas.put("selectionList", QuestionSelectionList);
            datas.put("selectResult", selectResult);
            datas.put("page", instance);
			
			return ResResult.build(200, "会员卡列表查询成功",datas);
		} catch (Exception e) {
			//返回错误代码
			e.printStackTrace();
			return ResResult.error(4, "会员卡列表查询失败");
		}
		
	}

}
